package com.qilin.hammer.util.database;
import static com.qilin.hammer.util.Global.ORCLE_DATABASE;

import java.sql.*;
import java.util.Map;

public class DBOperator {

	private Connection conn;
	private PreparedStatement pstmt = null;  
	
	public DBOperator(){}
	
	public DBOperator(String db_type, Map<String, String> params){
		
	}
	
	public DBOperator(Connection dbconn){
		this.conn = dbconn;
	}
	
	synchronized public boolean isExist(String reqname){
		String sql = "select tname from tab where tname = '"+reqname.toUpperCase()+"'";
		
		try{
			Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
			ResultSet resultset = stmt.executeQuery(sql);
			resultset.last();
			System.out.println(resultset.getRow());
			if(resultset.getRow()>0)
				return true;
			else
				return false;
		}catch(SQLException e){ 
			e.printStackTrace();
			return false;
		}
	}
	
	synchronized public void commit(){
		try{
			conn.commit();
		}catch(SQLException e){
			System.err.println("db commit error");
		}
	}
	
	synchronized public void createTable(String sql) { 
		try{
			Statement stmt = conn.createStatement();
			stmt.executeUpdate(sql);
		}catch(SQLException e){ 
			e.printStackTrace();
		}
	}
	
	
    //执行插入、删除和更新操作  
    synchronized public void execute(String sql) {  
        try{
        	pstmt = conn.prepareStatement(sql);  
        	if(pstmt != null)
        		pstmt.executeUpdate(sql);
        }catch(SQLException e){
        	System.err.println("db execute error");
        	e.printStackTrace();
        	try{
        		conn.rollback();
        	}catch(SQLException ee){
        		System.err.println("db rollback error");
        		ee.printStackTrace();
        	}
        }finally{
        	this.close(pstmt);
        }
    }  
    
    //执行查询操作  
    synchronized public ResultSet query(String sql) {  
    	ResultSet resultset = null ;  
    	try{
    		this.pstmt = conn.prepareStatement(sql);  
    		if(pstmt != null){  
    			resultset = pstmt.executeQuery(sql);  
    		}
    	}catch(SQLException e){
    		System.err.println("Query failed");
    		e.printStackTrace();
    	}finally{
    		this.close(pstmt);
        }
    	return resultset;
    }  
    
    public void close(Object obj){
    	if(obj==null)
    		return;
    	try{
    		if(obj instanceof java.sql.PreparedStatement)
    			((PreparedStatement)obj).close();
    		else if(obj instanceof ResultSet)
    			((ResultSet)obj).close();
    	}catch(SQLException e){
    		e.printStackTrace();
    	}
    }
    
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		DBFactory dbfactory = new DBFactory();
    	DBConnection orclconnector=dbfactory.getDBConnectionInstance(ORCLE_DATABASE);
    	Connection orclconn = orclconnector.getConnection("10.111.3.50", "1521", "ZHANGRM", "123456");
    	DBOperator orcloperator=new DBOperator(orclconn);
    	ResultSet rs=orcloperator.query("select * from shipmentproduct");
    	try{
    		while(rs.next()){
    			System.out.print(rs.getString(1)+" ");
    			System.out.print(rs.getString(2)+" ");
    			System.out.println(rs.getString(3)+" ");
    		}
    	}catch(SQLException e){
    		e.printStackTrace();
    	}
    	orclconnector.closeConnection(orclconn);
	}

}
